package com.atguigu.gulimall.cart.vo;

import java.math.BigDecimal;
import java.util.List;

// 购物车
public class Cart {
    List<CartItem> cartItems;
    private Integer countNum; // 商品的数量
    private Integer countType; // 商品的类型数量
    private BigDecimal totalAmount; // 商品的总价格
    private BigDecimal reduce = new BigDecimal("0.00"); // 减免价格

    public List<CartItem> getCartItems() {
        return cartItems;
    }

    public void setCartItems(List<CartItem> cartItems) {
        this.cartItems = cartItems;
    }

    public Integer getCountNum() {
        int count = 0;
        if(cartItems!=null && cartItems.size()>0){
            for (CartItem item : cartItems) {
                count += item.getCount();
            }
        }
        return count;
    }

    public Integer getCountType() {
        int count = 0;
        if(cartItems!=null && cartItems.size()>0){
            for (CartItem item : cartItems) {
                count += 1;
            }
        }
        return count;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        // 1.计算购物项总价
        if(cartItems!=null && cartItems.size()>0){
            for (CartItem item : cartItems) {
                if(item.getCheck()){
                    BigDecimal totalPrice = item.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }
        // 2.减免价格
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "cartItems=" + cartItems +
                ", countNum=" + countNum +
                ", countType=" + countType +
                ", totalAmount=" + totalAmount +
                ", reduce=" + reduce +
                '}';
    }
}
